package edu.sias.controller;

import edu.sias.pojo.Post;
import edu.sias.service.SearchService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//查看文章详情
@WebServlet(urlPatterns = "/post/details")
public class PostDetailsServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //需要判断访问者是否处于登录状态
        HttpSession session = request.getSession();
        //获取session中保存的成功状态码  k v
        Object o = session.getAttribute("status");
        //如果获取的状态码为空，说明当前用户未登录
        if (null == o){
            //跳转登录页面
            response.sendRedirect("/login.jsp");
        }else {
            //如果能获取到登录成功状态码，获取文章详情信息
            //。。。。。。。。。。
            String s = request.getParameter("id");
            int id = Integer.parseInt(s);
            //调用业务层进行根据id查询
            SearchService searchService = new SearchService();
            Post post = searchService.findPostById(id);
            //将获取到的信息保存到request中，便于前端获取
            request.setAttribute("post",post);
            //转发到展示文章信息详情页面
            request.getRequestDispatcher("/postInfo.jsp")
                    .forward(request,response);



        }

    }
}
